//
//  JLNetworkTool.swift
//  新浪微博
//
//  Created by linlin on 15/11/24.
//  Copyright © 2015年 jilin. All rights reserved.
//

import UIKit

import AFNetworking



//统一管理错误--自定义NSError
enum JLNetWorkToolError: Int {
    case EmptyAccessToken = -1
    case EmptyUid = -2
    
    //根据不同的枚举类型返回不同的属性
    var description: String {
        switch self {
        case .EmptyAccessToken:
            return "AccessToken没有值"
        case .EmptyUid:
            return "uid没有值"
        }
    }
    
    
    //根据当前类型返回一个NSError
    func error() ->NSError {
        return NSError(domain: "www.baidu.com", code: self.rawValue, userInfo: ["description" : self.description])
    }
    
    
}



///网络单例
class JLNetworkTool: AFHTTPSessionManager {

    //实现单例
    static let sharedNetworkTool: JLNetworkTool = {
        let baseURL = NSURL(string: "https://api.weibo.com/")
        let tool = JLNetworkTool(baseURL: baseURL)
        
        //添加序列化器-可以解析的类型
        tool.responseSerializer.acceptableContentTypes?.insert("text/plain")
        
        return tool
    }()
    
    
    
    let client_id = "567086706"
    
    let redirect_uri = "http://www.baidu.com/"
    
    let client_secret = "9c6f653b84b826fd20a3098924580e4d"
    
    //返回授权界面的URL地址
    func oauthURL() ->NSURL {
        //GET方式拼接登录界面请求地址
        let urlString = "https://api.weibo.com/oauth2/authorize?client_id=\(client_id)&redirect_uri=\(redirect_uri)"
        return NSURL(string: urlString)!
        
    }
    
    
    
    
    //加载AccessToken --返回数据
    func loadAccessToken(code: String, finished: (result: [String : AnyObject]?, error: NSError?) ->())  {
        let urlString = "https://api.weibo.com/oauth2/access_token"
        
        let parameters = [
            "client_id" : client_id,
            "client_secret" : client_secret,
            "grant_type" : "authorization_code",
            "code" : code,
            "redirect_uri" : redirect_uri
        ]
        
        
        //发送POST请求
        POST(urlString, parameters: parameters, success: { (_, result) -> Void in
            //返回数据
            finished(result: result as? [String : AnyObject], error: nil)
            }) { (_, error) -> Void in
                //返回错误信息
                finished(result: nil, error: error)
                
        }
        
    }
    
    
    
    //加载用户信息
    func loadAccountInfo (finished: (result: [String : AnyObject]?, error: NSError?) -> ()) {
        
        //判断沙盒是否有保存参数
        //守卫
        
        JLUserAccount.loadAccount()?.access_token == nil
        guard let access_token = JLUserAccount.loadAccount()?.access_token else {
            //access_token没有值
            print("access_token没有值")
            
            let error = JLNetWorkToolError.EmptyAccessToken.error()
            
            finished(result: nil, error: error)
            print("\(error)")
            
            return
        }
        
        guard let uid = JLUserAccount.loadAccount()?.uid else {
            //uid没有值
            print("uid没有值")
            
            let error = JLNetWorkToolError.EmptyUid.error()
            
            finished(result: nil, error: error)
            
            return

        }
        
                
        //发送请求
        let urlString = "2/users/show.json"
        
        let parameters = [
            "access_token" : access_token,
            "uid" : uid
        ]
        
        GET(urlString, parameters: parameters, success: { (_, result) -> Void in
            finished(result: result as? [String : AnyObject], error: nil)
            
            }) { (_, error) -> Void in
            finished(result: nil, error: error)
        }
        
        
        
        
        
    }
    
    
    
    
    

    
}
